package logic.action;

import java.util.List;

import logic.action.base.AbstractAsyncUIData;
import logic.action.base.AsyncUIAction;
import logic.constants.DefaultConsts;
import logic.dao.BookMarksDao;

import org.geometerplus.fbreader.book.Bookmark;

import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import base.tina.core.task.Task;
import base.tina.core.task.TaskService;
import base.tina.core.task.infc.ITaskResult;

import com.shwread.android.qysw.activity.BaseBusinessActivity;


public class GetOneBooknotesAction<T extends BaseBusinessActivity>
        extends
        AsyncUIAction<T>
{
	
	OneBookNotesListener oneBookNotesListener;
	
	public GetOneBooknotesAction(T baseActivity) {
		super(baseActivity);
	}
	
	@Override
	public boolean exCaught(ITaskResult arg0, TaskService arg1) {
		boolean isHandled = true;
		switch (arg0.getSerialNum()) {
			case GetOneBooknotesTask.SerialNum:
				//#debug error
				base.tina.core.log.LogPrinter.e(null, "~~~~~~~~GetOneBooknotesTask error~~~~~~~~~~~");
				
				break;
			default:
				isHandled = false;
				break;
		}
		return isHandled;
	}
	
	@Override
	public boolean handleResult(ITaskResult arg0, TaskService arg1) {
		boolean isHandled = true;
		switch (arg0.getSerialNum()) {
			case GetOneBooknotesResult.SerialNum:
				GetOneBooknotesResult result = (GetOneBooknotesResult) arg0;
				oneBookNotesListener.setAllBookInfos(result.list);
				Message msg = Message.obtain();
				msg.what = DefaultConsts.UPDATEUI_GETONEBOOK_BOOKNOTES;
				bActivity.getUIHandler().sendMessage(msg);
				
				break;
			default:
				isHandled = false;
				break;
		}
		return isHandled;
		
	}
	
	@Override
	public void create() {
		
	}
	
	@Override
	public <E> void start(E e) {
		Bundle bundle = (Bundle) e;
		String bookDbId = bundle.getString(DefaultConsts.db_id_s);
		String contentId = bundle.getString(DefaultConsts.book_contentId_s);
		String title = bundle.getString(DefaultConsts.book_name);
		bActivity.mAService.requestService(new GetOneBooknotesTask(bActivity, bookDbId, contentId, title), getBindSerial());
	}
	
	public void OnSearchInfoListener(OneBookNotesListener bookNotesListener) {
		this.oneBookNotesListener = bookNotesListener;
	}
	
	public interface OneBookNotesListener
	{
		public void setAllBookInfos(List<Bookmark> infos);
	}
	
	public class GetOneBooknotesTask
	        extends
	        Task
	{
		
		Context context;
		String  contentId;
		String  bookDbId;
		String  title;
		
		public GetOneBooknotesTask(Context context, String bookDbId, String contentId, String title) {
			super(0);
			this.context = context;
			this.bookDbId = bookDbId;
			this.contentId = contentId;
			this.title = title;
		}
		
		@Override
		public void run() throws Exception {
			GetOneBooknotesResult result = new GetOneBooknotesResult();
			result.list = BookMarksDao.getInstance(context).loadOneBookNotes(bookDbId, contentId, title);
			
			commitResult(result, CommitAction.WAKE_UP);
		}
		
		@Override
		public int getSerialNum() {
			return SerialNum;
		}
		
		public static final int SerialNum = -GetOneBooknotesResult.SerialNum;
		
	}
	
	public static class GetOneBooknotesResult
	        extends
	        AbstractAsyncUIData
	{
		List<Bookmark> list;
		
		@Override
		public int getSerialNum() {
			return SerialNum;
		}
		
		public static final int SerialNum = GET_ONEBOOK_BOOKNOTES;
	}
	
}
